package com.lab.network.model;

import com.lab.network.config.CachePolicyEnum;
import com.lab.network.config.MethodEnum;
import com.lab.network.config.ProtocolEnum;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by ak
 * <p/>
 */
public class CtRemoteBusiness {
    private static final String TAG = "XisMiRemoteBusiness";

    /**
     * 请求对象
     */
    public CtApiRequest request;
    /**
     * 请求参数属性对象
     */
    public CtRequestProp ctRequestProp = new CtRequestProp();

    public CtRemoteBusiness(CtApiRequest request) {
        this.request = request;
    }



    /**
     * 使用配置的缓存策略
     */
    public CtRemoteBusiness useCache(CachePolicyEnum cachePolicyEnum) {
        this.ctRequestProp.setCacheModel(cachePolicyEnum);
        return this;
    }

    /**
     * 设置请求发送的方法，目前暂时支持GET或POST方法。
     */
    public CtRemoteBusiness reqMethod(MethodEnum method) {
        if (null != method) {
            this.ctRequestProp.setMethod(method);
        }
        return this;
    }

    /**
     * 设置产生服务结果缓存时，根据request生成cachekey时，需要剔除的api业务字段
     *
     * @param blackList 剔除字段列表
     * @return
     */
    public CtRemoteBusiness setCacheKeyParamBlackList(Set<String> blackList) {
        if (null != blackList) {
            this.ctRequestProp.cacheKeyBlackList = blackList;
        }
        return this;
    }

    /**
     * 设置服务通信的请求头
     */
    public CtRemoteBusiness headers(Map<String, String> requestHeaders) {
        if ((requestHeaders != null) && (!requestHeaders.isEmpty())) {
            if (this.ctRequestProp.getRequestHeaders() != null) {
                this.ctRequestProp.getRequestHeaders().putAll(requestHeaders);
            } else {
                this.ctRequestProp.addRequestHeaders(requestHeaders);
            }
        }
        return this;
    }

    /**
     * 设置请求头：cache-control=no-cache
     * 注意：发送GET请求时可以调用此方法设置请求头；发送POST请求时不建议调用此方法设置请求头；
     */
    public CtRemoteBusiness setCacheControlNoCache() {
        Map<String, String> requestHeaders = this.ctRequestProp.getRequestHeaders();
        if (requestHeaders == null) {
            requestHeaders = new HashMap<String, String>();
        }
//        requestHeaders.put(HttpHeaderConstant.CACHE_CONTROL, HttpHeaderConstant.NO_CACHE);
        this.ctRequestProp.addRequestHeaders(requestHeaders);
        return this;
    }

    /**
     * 设置请求协议
     *
     * @param protocol 协议头信息
     */
    public CtRemoteBusiness protocol(ProtocolEnum protocol) {
        if (protocol != null) {
            this.ctRequestProp.setProtocol(protocol);
        }
        return this;
    }

    /**
     * 获取request cache标示符
     *
     * @return 返回request产生的缓存key
     */
    public String getCacheKey() {
        return "";
    }


    public String getCahceKeyOrigin(){
        return "";
    }
}
